﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WechatModule.Tool;

namespace WechatModule.API
{
    /// <summary>
    /// 模版信息Api
    /// </summary>
    public class TemplateMsgAPI
    {
        /// <summary>
        /// 发送模版信息
        /// </summary>
        /// <returns></returns>
        public static string SendTo()
        {
            string createMenuUrl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
            createMenuUrl = createMenuUrl.Replace("ACCESS_TOKEN", WechatBaseAPI.GetAccessToken());
            #region 注释
            /*
          {
                    "touser": "ofaHdt8LhL3jJv6pQWhIm9gpJEdU",
                    "template_id": "3SwfUqbchGZEjPgeIe0iMv9-KMDid40KVIREARZqKUI",
                    "url": "http://weixin.qq.com/download",
                    "data":
                    {
                        "a":
                        {
                            "value": "恭喜你购买成功！",
                            "color": "#173177"
                        }
                    }
                }";*/
            #endregion
            string responeJsonStr = @"
{
                    'touser': 'ofaHdt8LhL3jJv6pQWhIm9gpJEdU',
                    'template_id':'pUipgqIkbUOuet5ulS7t6RoFFfCDCU2xt8B7vn29hMQ',
                    'url': 'http://weixin.qq.com/download',
                    'data':
                    {
                        'first':
                        {
                            'value': '您好！',
                            'color': '#173177'
                        },
                        'product':
                        {
                            'value': '拉菲2000！',
                            'color': '#173177'
                        },
                        'price':
                        {
                            'value': '100',
                            'color': '#173177'
                        },
                        'time':
                        {
                            'value': '测试时间！',
                            'color': '#173177'
                        },         
                        'remark':{
                            'value':'欢迎再次购买！',
                            'color':'#173177'
                        }   
                    }
}";
            byte[] bytes = Encoding.UTF8.GetBytes(responeJsonStr.Replace("'", "\"").ToString());


            WebRequest req = WebRequest.Create(createMenuUrl);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bytes.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bytes, 0, bytes.Length);
                reqStream.Flush();
            }
            using (var response = req.GetResponse())
            {
                using (StreamReader rea = new StreamReader(response.GetResponseStream()))
                {
                    return rea.ReadToEnd();
                }
            }
        }
        /// <summary>
        /// 得到模版信息
        /// </summary>
        /// <returns></returns>
        public static string GetTemplate()
        {
            string apiUrl = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
            apiUrl = apiUrl.Replace("ACCESS_TOKEN", WechatBaseAPI.GetAccessToken());
            return ReuqestTool.GetWechatJson(apiUrl);
        }
        /// <summary>
        /// 设置行业
        /// </summary>
        /// <returns></returns>
        public static string SetIndustry(int industry_id1,int industry_id2)
        {
            string apiUrl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
            apiUrl = apiUrl.Replace("ACCESS_TOKEN", WechatBaseAPI.GetAccessToken());
            #region 注释
            /*
           {
          "industry_id1":"1",
          "industry_id2":"4"
       }*/
            #endregion
            string responeJsonStr = @"
  {
          'industry_id1':'"+industry_id1+@"',
          'industry_id2':'"+industry_id2+@"'
       }
";
            byte[] bytes = Encoding.UTF8.GetBytes(responeJsonStr.Replace("'", "\"").ToString());
            WebRequest req = WebRequest.Create(apiUrl);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bytes.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bytes, 0, bytes.Length);
                reqStream.Flush();
            }
            using (var response = req.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}
